﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using WF.BusinessObject;

namespace WF.Security
{
    public class WFAurthorizationModule : IHttpModule
    {
        #region IHttpModule Members

        /// <summary>
        /// 
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        public void Init(HttpApplication context)
        {
            context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
            context.AcquireRequestState += new EventHandler(context_AcquireRequestState);
        }

        /// <summary>
        /// Writting accessing log
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void context_AcquireRequestState(object sender, EventArgs e)
        {
            string userName;
            if ((null != System.Web.HttpContext.Current.User) && (null != System.Web.HttpContext.Current.User.Identity))
            {
                userName = System.Web.HttpContext.Current.User.Identity.Name;
            }
            else
            {
                userName = "";
            }
            SecurityManager sm = new SecurityManager();
            bool IsAuthorized = sm.IsAuthorized(userName, ((HttpApplication)sender).Request.AppRelativeCurrentExecutionFilePath);
            WF.Common.TraceHelper.TraceInformation("UserName={0}; Url={1}; IsAuthorized={2}", userName, ((HttpApplication)sender).Request.AppRelativeCurrentExecutionFilePath, IsAuthorized);
            if (!IsAuthorized)
            {
                //((HttpApplication)sender).Server.Transfer("/AccessDenied.htm");
                throw new Common.BusinessException(string.Format("你没有权限访问页面 {0}", ((HttpApplication)sender).Request.AppRelativeCurrentExecutionFilePath));
            }
        }

        /// <summary>
        /// Checking url authorization
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void context_AuthorizeRequest(object sender, EventArgs e)
        {
            //string userName;
            //if ((null != System.Web.HttpContext.Current.User) && (null != System.Web.HttpContext.Current.User.Identity))
            //{
            //    userName = System.Web.HttpContext.Current.User.Identity.Name;
            //}
            //else
            //{
            //    userName = "";
            //}
            //SecurityManager sm = new SecurityManager();
            //bool IsAuthorized = sm.IsAuthorized(userName, ((HttpApplication)sender).Request.AppRelativeCurrentExecutionFilePath);
            //CCHR.Common.TraceHelper.TraceInformation("UserName={0}; Url={1}; IsAuthorized={2}", userName, ((HttpApplication)sender).Request.AppRelativeCurrentExecutionFilePath, IsAuthorized);
            //if (!IsAuthorized)
            //{
            //    ((HttpApplication)sender).Server.Transfer("/AccessDenied.htm");
            //}

        }
        #endregion
    }
}
